<template>
    <div class="forget_container">
        <div class="forget_box">
            <!--登录表单区域-->
            <el-form ref="forgetFormRef" :model="forgetForm" :rules="forgetFormRules" label-width="0px"
                class="forget_form">
                <!--用户名-->
                <el-form-item prop="id">
                    <el-input v-model="forgetForm.id" prefix-icon="user" placeholder="用户名"></el-input>
                </el-form-item>
                <!--问题-->
                <el-form-item prop="question">
                    <el-input v-model="forgetForm.question" prefix-icon="lock" placeholder="问题" type="question"
                        disabled>
                    </el-input>
                </el-form-item>
                <!--答案-->
                <el-form-item prop="ans">
                    <el-input v-model="forgetForm.ans" prefix-icon="key" placeholder="答案" type="ans">
                    </el-input>
                </el-form-item>
                <!--按钮区域-->
                <el-form-item class="btns">
                    <el-button type="success" @click="getQuestion">获取问题</el-button>
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            forgetForm: {
                id: '',
                passwd: '',
                question: '',
                ans: '',
            },
            //表单的验证规则对象
            forgetFormRules: {
                id: [
                    { required: true, message: "请输入用户名", trigger: "blur" }
                ],
                ans: [
                    { required: true, message: "请输入答案", trigger: "blur" }
                ]
            }
        }
    },
    methods: {
        //重置表单
        resetLoginForm() {
            this.$refs.forgetFormRef.resetFields();
        },
        //表单预验证
        login() {
            this.$refs.forgetFormRef.validate(async valid => {
                if (!valid) return;
                const json = {
                    id: this.forgetForm.id,
                    ans: this.forgetForm.ans
                };
                const { data: res } = await this.$http.post("checkAnswer", json);
                window.sessionStorage.setItem("token", res.token);
                if (res.error_code !== 0) {
                    return this.$message.error('登录失败');
                }
                this.$message.success('登录成功');
                if (res.type === 0) {
                    this.$router.push('/home_emp');
                } else if (res.type === 1) {
                    this.$router.push('/home_HR');
                } else if (res.type === 2) {
                    this.$router.push('/home_com');
                } else {
                    this.$router.push('/home_admin');
                }
            });
        },
        async getQuestion() {
            const json = {
                id: this.forgetForm.id,
                passwd: ''
            };
            const { data: res } = await this.$http.post('question', json)
            if (res.error_code !== 0) {
                return this.$message.error('获取问题失败！')
            }
            this.forgetForm.question = res.question;
        }
    }
};
</script>

<style lang="less" scoped>
.forget_container {
    background-color: #2b4b6b;
    height: 100%;
    background-image: url("../assets/1.jpg");
    background-Repeat:'no-repeat';
}

.forget_box {
    width: 450px;
    height: 300px;
    background-color: rgba(255, 255, 255, 0.5);
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

.forget_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
.btns {
    display: flex;
    justify-content: flex-end;
    float: right;
}
</style>
    
